<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./libs/vue3.js"></script>
    <style>
        .modal {
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 500px;
            height: 300px;
            border: 10px solid #F2F2F2;
            border-radius: 10px;
        }

        .btn-close {
            position: absolute;
            bottom: 10px;
            left: 50%;
            transform: translateX(-50%);
        }
    </style>
</head>

<body>
    <div id="app">
        <button @click="visible=!visible">toggle</button>
        <!-- 模态框组件(对话框组件) -->
        <!-- 监听自定义事件close, 通过事件处理函数closeModal, 接收子组件发送的数据 -->
        <!-- <modal :modelValue="visible" @update:modelValue="closeModal"/> -->
        <!-- v-model:
             1-在组件的调用标签上添加一个自定义属性modelValue, 通过该属性将数据传递给子组件
             2-在组件的调用标签上监听自定义事件update:modelValue="visible=$event"
        -->
        <modal v-model="visible" />
    </div>
</body>
<script>
    

    const app = Vue.createApp({
        data() {
            return {
                // 控制模态框组件的显示状态, 默认隐藏
                visible: false
            }
        }
    });


    // 注册全局组件
    app.component('modal', {
        props: ['modelValue'],
        template: `
            <div class="modal" v-show="modelValue">
                <button class="btn-close" @click="close">关闭</button>
            </div>
        `,
        methods: {
            close() {
                // 触发一个自定义事件, 通知父组件来关闭模态框
                this.$emit('update:modelValue', false);
            }
        }
    });

   app.mount('#app');
</script>

</html>